/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.Serializable;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.*;
import pojo.Nhanvien;
import util.ExceptionHandle;
import util.HibernateUtil;

/**
 *
 * @author HoaDinhGiang
 */
public class nhanvienDAO {

    static Logger log = Logger.getLogger(nhanvienDAO.class);

    public static List<Nhanvien> timKiemNhanVien(Nhanvien hh) {

        List<Nhanvien> ds = null;

        int maNhanVien = hh.getManhanvien();
        String diaChi = hh.getDiachi();
        String dienThoai = hh.getDienthoai();
        String eMail = hh.getEmail();
        String nganHang = hh.getNganhang();
        String soTK = hh.getSotaikhoan();
        String tenNV = hh.getTennhanvien();
        int maBoPhan = hh.getBophan().getMabophan();
        //  hh.getLoaihanghoa().getMaloaihanghoa();
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select hh from Nhanvien hh where (hh.manhanvien = " + maNhanVien
                    + "or lower(hh.tennhanvien) like lower('" + tenNV + "')"
                    + "or hh.dienthoai = '" + dienThoai + "'"
                    + "or hh.email = '" + eMail + "'"
                    + "or hh.nganhang = '" + nganHang + "'"
                    + "or hh.sotaikhoan = '" + soTK + "'"
                    + "or hh.diachi = '" + diaChi + "'"
                    + "or hh.bophan.mabophan = " + maBoPhan
                    + ")and hh.kichhoat = true";
            //     + "or hh.loaihanghoa.maloaihanghoa = "+maLoai;
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<Nhanvien> layDanhSachNhanVienTonTai() {
        List<Nhanvien> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from Nhanvien nv where nv.kichhoat = true ORDER BY nv.manhanvien ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<Nhanvien> layDanhSachNhanVienThuKho() {
        List<Nhanvien> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from Nhanvien nv where nv.kichhoat = true and nv.bophan.mabophan = '5' ORDER BY nv.manhanvien ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static List<Nhanvien> layDanhSachNhanVienDaXoa() {
        List<Nhanvien> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select nv from Nhanvien nv where nv.kichhoat = false ORDER BY nv.manhanvien ASC";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return ds;
    }

    public static Nhanvien layThongTinNhanVien(String maNV) {
        Nhanvien hh = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            hh = (Nhanvien) session.get(Nhanvien.class, maNV);
            Hibernate.initialize(hh.getBophan());
        } catch (HibernateException ex) {
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return hh;
    }

    public static boolean themNhanVien(Nhanvien nv) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            Serializable save = session.save(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return true;
    }

    public static boolean capNhatThongTinNhanVien(Nhanvien nv) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return true;
    }

    public static void xoaTamThoi(Nhanvien nv) {
        /*
         Session session =  HibernateUtil.getSessionFactory().openSession();
         try{
         String hql = "update Cuahang ch set kichhoat = false where ch.macuahang = "+maCuaHang;
         System.out.println(hql);
         Query query = session.createQuery(hql);
         int k = query.executeUpdate();
         System.out.print(k);
         }catch(HibernateException ex){
         //Log the exception
         System.err.println(ex);
         }finally{
         session.close();
         }
         * 
         */
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
    }

    public static boolean xoaVinhVien(String maNhanVien) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Nhanvien nv = nhanvienDAO.layThongTinNhanVien(maNhanVien);
        if (nv == null) {
            return false;
        }
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(nv);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            log.error(ex);
            ExceptionHandle.logException(ex);
        } finally {
            session.close();
        }
        return true;
    }
}
